#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QFileDialog>
#include <QDebug>
#include "PreviewPix.h"

const int PIX_COUNT = 5;
const int PIX_SIZE = 100;

PreviewPix::PreviewPix(QWidget *parent)
    : QDialog(parent),
      currentDirString(),
      currentPageInt(0),
      pageCountInt(0)
{
    chooseDir_PushButton = new QPushButton(tr("..."));
    pageUp_PushButton = new QPushButton(tr("PageUp"));
    pageDown_PushButton = new QPushButton(tr("PageDown"));

    currentDir_LineEdit = new QLineEdit;

    readPix_Thread = new ReadPix(this);

    currentDir_LineEdit->setEnabled(false);

    QGridLayout* pix_Layout = new QGridLayout;
    for(int r = 0 ; r < PIX_COUNT ; ++r)
    {
        for(int c = 0 ; c < PIX_COUNT ; ++c)
        {
            previewPixList.append(new QPushButton);
            previewPixList.last()->setFixedSize(PIX_SIZE,PIX_SIZE);
            pix_Layout->addWidget(previewPixList.last(),r,c);
        }

    }

    QHBoxLayout* dir_Layout = new QHBoxLayout;
    dir_Layout->addWidget(currentDir_LineEdit);
    dir_Layout->addWidget(chooseDir_PushButton);
    QHBoxLayout* button_Layout = new QHBoxLayout;
    button_Layout->addStretch();
    button_Layout->addWidget(pageUp_PushButton);
    button_Layout->addStretch();
    button_Layout->addWidget(pageDown_PushButton);
    button_Layout->addStretch();
    QVBoxLayout* main_Layout = new QVBoxLayout;
    main_Layout->addLayout(dir_Layout);
    main_Layout->addLayout(pix_Layout);
    main_Layout->addLayout(button_Layout);
    setLayout(main_Layout);
    main_Layout->setSizeConstraint(QLayout::SetFixedSize);

    connect(chooseDir_PushButton,SIGNAL(clicked()),this,SLOT(changeCurrentDir()));
    connect(readPix_Thread,SIGNAL(loadPix(const QPixmap& , int)),this,SLOT(addPixOneByOne(const QPixmap& , int)));
    connect(pageUp_PushButton,SIGNAL(clicked()),this,SLOT(pageUp()));
    connect(pageDown_PushButton,SIGNAL(clicked()),this,SLOT(pageDown()));
}

PreviewPix::~PreviewPix()
{
    readPix_Thread->stopLoadPix();
    readPix_Thread->exit();
}

void PreviewPix::resetThis()
{
    pixList.clear();
    for(auto A : previewPixList)  //每次更换目录就把图片设为默认图片
    {
        A->setIcon(QPixmap(tr(":/images/EmptyPix.png")));
        A->setIconSize(QSize(PIX_SIZE,PIX_SIZE));
    }
    currentPageInt = 0;
    pageCountInt = 0;
}

void PreviewPix::changeCurrentDir()
{
    QString newDir = QFileDialog::getExistingDirectory(this,tr("Open New Dir"),tr("."));
    if(newDir.isEmpty())
        return;
    else
        currentDirString = newDir;
    currentDir_LineEdit->setText(currentDirString);
    resetThis();


    QList<QString> pixNames;
    QDir dirs(currentDirString);
    int cs = dirs.count();
    for(int i = 0 ; i < cs ; ++i)
    {
        QString names = dirs[i];
        QString nameTail = names.right(3);
        if(nameTail == tr("png") || nameTail == tr("jpg") || nameTail == tr("bmp"))
        {
            pixNames.append(currentDirString + tr("/") + names);
            pixList.append(QPixmap(tr(":/images/EmptyPix.png"))); //没读到一个图片，就放入背景图片，等加载完了再换成加载的图片
        }
    }
    pageCountInt = cs / (PIX_COUNT*PIX_COUNT);
    pageCountInt += (cs % (PIX_COUNT*PIX_COUNT) == 0) ? 0 : 1; //每页可以放25张，如果不满一页，就加一页

    readPix_Thread->stopLoadPix();
    readPix_Thread->exit();
    readPix_Thread->setPixName(pixNames);
    readPix_Thread->start();
}

void PreviewPix::addPixOneByOne(const QPixmap& pix , int indexs)
{
    pixList[indexs] = pix;
    if(indexs < PIX_COUNT*PIX_COUNT)
    {
        QPixmap pPix = pix.scaled(PIX_SIZE,PIX_SIZE);
        previewPixList.at(indexs)->setIcon(QIcon(pPix));
        previewPixList.at(indexs)->setIconSize(QSize(PIX_SIZE,PIX_SIZE));
    }
}

void PreviewPix::pageUp()
{
    if(currentPageInt > 0)
        --currentPageInt;
    int cs = pixList.size();
    for(int i = 0 ; i < PIX_COUNT*PIX_COUNT ; ++i)
    {
        int indexs = currentPageInt*PIX_COUNT*PIX_COUNT + i;
        if(indexs >= cs)
            previewPixList.at(i)->setIcon(QIcon());
        else
        {
            previewPixList.at(i)->setIcon(pixList.at(indexs));
            previewPixList.at(i)->setIconSize(QSize(PIX_SIZE,PIX_SIZE));
        }
    }
}

void PreviewPix::pageDown()
{
    if(currentPageInt < pageCountInt - 1)
        ++currentPageInt;
    int cs = pixList.size();
    for(int i = 0 ; i < PIX_COUNT*PIX_COUNT ; ++i)
    {
        int indexs = currentPageInt*PIX_COUNT*PIX_COUNT + i;
        if(indexs >= cs)
            previewPixList.at(i)->setIcon(QIcon());
        else
        {
            previewPixList.at(i)->setIcon(pixList.at(indexs));
            previewPixList.at(i)->setIconSize(QSize(PIX_SIZE,PIX_SIZE));
        }
    }
}